package com.yuexun.beilunpatient.ui.registration.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.main.ui.Act_Webview;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentPatientDetail;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentRegisterPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentRegisterView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/11/13.
 */
public class Act_Appointment_Register extends BaseKJActivity implements IAppointmentRegisterView {

    @Bind(R.id.tv_username)
    TextView tvUsername;
    @Bind(R.id.tv_identity)
    TextView tvIdentity;
    @Bind(R.id.treatmentCard)
    EditText treatmentCard;
    @Bind(R.id.loginName)
    EditText loginName;
    @Bind(R.id.tv_phone)
    EditText tvPhone;
    @Bind(R.id.tv_address)
    EditText tvAddress;
    @Bind(R.id.tv_pw)
    EditText tvPw;
    @Bind(R.id.tv_repw)
    EditText tvRepw;
    @Bind(R.id.isCheck)
    CheckBox isCheck;
    @Bind(R.id.agreement)
    TextView agreement;
    @Bind(R.id.next)
    Button next;

    AppointmentRegisterPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_appointment_register);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new AppointmentRegisterPresenter(new RegistrationModel(), this);
//        tvUsername.setText(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_NAME));
//        tvIdentity.setText(PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ID_CARD));
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.loadPatientInfoDetail(params);
    }

    private boolean isCard(String card){
        if(card.length() == 9){
            return true;
        }
        if(card.length() > 4 && card.substring(0,4).equals("0574")) {
            return true;
        }
        return false;
    }


    @OnClick({R.id.title_back, R.id.isCheck, R.id.agreement, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.isCheck:
                break;
            case R.id.agreement:
//                Uri uri = Uri.parse("https://51wangzhen.com/app/app_agreement.html");
//                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//                startActivity(intent);
                Intent intent = new Intent(aty, Act_Webview.class);
                intent.putExtra("url", "https://51wangzhen.com/app/app_agreement.html");
                intent.putExtra("title", "注册协议");
                startActivity(intent);
                break;
            case R.id.next:
                if (loginName.getText().toString().isEmpty()) {
                    ViewInject.toast("登录名不为空");
                    break;
                }
                if (!isCard(treatmentCard.getText().toString())) {
                    ViewInject.toast("就诊卡号不正确");
                    break;
                }
                if (treatmentCard.getText().toString().isEmpty()) {
                    ViewInject.toast("就诊卡号不为空");
                    break;
                }
                if (tvPhone.getText().toString().isEmpty()) {
                    ViewInject.toast("手机号不为空");
                    break;
                }
                if (tvAddress.getText().toString().isEmpty()) {
                    ViewInject.toast("家庭住址不为空");
                    break;
                }
                if (tvPw.getText().toString().isEmpty()) {
                    ViewInject.toast("密码不为空");
                    break;
                }
                if (tvPw.getText().toString().length() < 8) {
                    ViewInject.toast("密码不少于8位");
                    break;
                }
                if (!tvPw.getText().toString().equals(tvRepw.getText().toString())) {
                    ViewInject.toast("密码不一致");
                    break;
                }
                if (!isCheck.isChecked()) {
                    ViewInject.toast("请仔细读阅注册协议并勾选");
                    break;
                }
                Map<String, String> params = new HashMap<>();
                params.put("address", tvAddress.getText().toString());
                params.put("phone", tvPhone.getText().toString());
                params.put("treatmentCard", treatmentCard.getText().toString());
                params.put("idcard", tvIdentity.getText().toString());
                params.put("name", tvUsername.getText().toString());
                params.put("loginPassword", tvPw.getText().toString());
                params.put("loginName", loginName.getText().toString());
                params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                presenter.appointmentRegister(params);
                break;
        }
    }

    @Override
    public void appointmentRegister(BaseEntity<String> baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            Bundle bundle = new Bundle();
            bundle.putString("phone", tvPhone.getText().toString());
            bundle.putString("password", tvPw.getText().toString());
            bundle.putString("loginName", loginName.getText().toString());
            Intent intent = new Intent(aty, Act_Appointment_Auth.class);
            intent.putExtras(bundle);
            startActivity(intent);
        } else {
            ViewInject.toast(baseEntity.getMessage());
        }
    }

    @Override
    public void loadPatientInfoDetail(BaseEntity<AppointmentPatientDetail> baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            tvUsername.setText(baseEntity.getDatas().get(0).getPatientName());
            tvIdentity.setText(baseEntity.getDatas().get(0).getIdCardNo());
            treatmentCard.setText("");
            tvPhone.setText(baseEntity.getDatas().get(0).getMobile());
            tvAddress.setText(baseEntity.getDatas().get(0).getAddress());
        } else {
            ViewInject.toast(baseEntity.getMessage());
        }
    }
}
